package com.softline.documentsmall.service.impl;

import com.softline.documentsmall.dao.DictionaryMapper;
import com.softline.documentsmall.dao.EntryDetailMapper;
import com.softline.documentsmall.entity.Dictionary;
import com.softline.documentsmall.entity.EntryDetails;
import com.softline.documentsmall.ret.BusinessErrorEnum;
import com.softline.documentsmall.ret.ReturnVO;
import com.softline.documentsmall.ret.ReturnVOUtils;
import com.softline.documentsmall.service.EntryDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhongweicheng
 * @date 2020/9/9 9:54
 */
@Service
@Slf4j
public class EntryDetailsServiceImp implements EntryDetailService {

    @Autowired
    EntryDetailMapper entryDetailMapper;

    @Autowired
    DictionaryMapper dictionaryMapper;

    @Value(value = "${pageParams.currPage}")
    private Integer currPageCfg;

    @Value(value = "${pageParams.pageSize}")
    private Integer pageSizeCfg;

    @Override
    public ReturnVO findByParams(EntryDetails entryDetails) {
        List<EntryDetails> entryDetailsList = entryDetailMapper.findByParams(entryDetails,(currPageCfg-1)*pageSizeCfg,pageSizeCfg);
        return ReturnVOUtils.success(entryDetailsList);
    }

    @Override
    public ReturnVO insertEntryDetails(EntryDetails entryDetails) {
        try {
            entryDetailMapper.insertEntryDetail(entryDetails);
        }catch (Exception e){
            log.warn("EntryDetailsServiceImp insertEntryDetails exception:{}",e.getMessage());
            return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
        }
        return ReturnVOUtils.success();
    }

    @Override
    public ReturnVO updateById(EntryDetails entryDetails) {

        try {
            entryDetailMapper.updateEntryDetailById(entryDetails);
        }catch (Exception e){
            log.warn("EntryDetailsServiceImp updateById exception:{}",e.getMessage());
            return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
        }
        return ReturnVOUtils.success();
    }

    @Override
    public ReturnVO softDeleteById(Integer id) {
        try {
            entryDetailMapper.softDeleteEntryDetailById(id);
        }catch (Exception e){
            log.warn("EntryDetailsServiceImp softDeleteById exception:{}",e.getMessage());
            return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
        }
        return ReturnVOUtils.success();
    }

    @Override
    public EntryDetails findByDictionaryIdLanguageTitle(Integer dictionaryId, String language, String title) {
      return entryDetailMapper.findByDictionaryIdLanguageTitle(dictionaryId,language,title);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch() {
        //获取所有词条目录
        List<Dictionary> dictionaryList = dictionaryMapper.findAll();
        //循环为每一个词条新增默认目录：术语解读（十一种语言）
        for (int i = 0; i < dictionaryList.size(); i++) {
            Dictionary dictionary = dictionaryList.get(i);
            //根据词条id和排序查询记录，术语解读拍排序为1
            List<EntryDetails> entryDetailsList = entryDetailMapper.findByDictionaryIdAndSort(dictionary.getId());
            //如果entryDetailsList不为空说明此词条已经有默认目录，不用新增了
            if (entryDetailsList.size() == 0) {
                //中文
                EntryDetails cnEntryDetails = new EntryDetails();
                cnEntryDetails.setDictionaryId(dictionary.getId());
                cnEntryDetails.setTitle("术语解读");
                cnEntryDetails.setIsDelete(0);
                cnEntryDetails.setSort(1);
                cnEntryDetails.setLanguage("cn");
                entryDetailMapper.insertEntryDetail(cnEntryDetails);
                //英文
                EntryDetails enEntryDetails = new EntryDetails();
                enEntryDetails.setDictionaryId(dictionary.getId());
                enEntryDetails.setTitle("Interpretation of terms");
                enEntryDetails.setIsDelete(0);
                enEntryDetails.setSort(1);
                enEntryDetails.setLanguage("en");
                entryDetailMapper.insertEntryDetail(enEntryDetails);
                //阿拉伯语
                EntryDetails arEntryDetails = new EntryDetails();
                arEntryDetails.setDictionaryId(dictionary.getId());
                arEntryDetails.setTitle("تفسير المصطلحات");
                arEntryDetails.setIsDelete(0);
                arEntryDetails.setSort(1);
                arEntryDetails.setLanguage("ar");
                entryDetailMapper.insertEntryDetail(arEntryDetails);
                //法语
                EntryDetails frEntryDetails = new EntryDetails();
                frEntryDetails.setDictionaryId(dictionary.getId());
                frEntryDetails.setTitle("Terminologie");
                frEntryDetails.setIsDelete(0);
                frEntryDetails.setSort(1);
                frEntryDetails.setLanguage("fr");
                entryDetailMapper.insertEntryDetail(frEntryDetails);
                //德语
                EntryDetails deEntryDetails = new EntryDetails();
                deEntryDetails.setDictionaryId(dictionary.getId());
                deEntryDetails.setTitle("Auslegung der Begriffe");
                deEntryDetails.setIsDelete(0);
                deEntryDetails.setSort(1);
                deEntryDetails.setLanguage("de");
                entryDetailMapper.insertEntryDetail(deEntryDetails);
                //意大利语
                EntryDetails itEntryDetails = new EntryDetails();
                itEntryDetails.setDictionaryId(dictionary.getId());
                itEntryDetails.setTitle("Interpretazione dei termini");
                itEntryDetails.setIsDelete(0);
                itEntryDetails.setSort(1);
                itEntryDetails.setLanguage("it");
                entryDetailMapper.insertEntryDetail(itEntryDetails);
                //日本语
                EntryDetails jpEntryDetails = new EntryDetails();
                jpEntryDetails.setDictionaryId(dictionary.getId());
                jpEntryDetails.setTitle("用語の解読");
                jpEntryDetails.setIsDelete(0);
                jpEntryDetails.setSort(1);
                jpEntryDetails.setLanguage("jp");
                entryDetailMapper.insertEntryDetail(jpEntryDetails);
                //韩语
                EntryDetails krEntryDetails = new EntryDetails();
                krEntryDetails.setDictionaryId(dictionary.getId());
                krEntryDetails.setTitle("용어 해석");
                krEntryDetails.setIsDelete(0);
                krEntryDetails.setSort(1);
                krEntryDetails.setLanguage("kr");
                entryDetailMapper.insertEntryDetail(krEntryDetails);
                //葡萄牙语
                EntryDetails ptEntryDetails = new EntryDetails();
                ptEntryDetails.setDictionaryId(dictionary.getId());
                ptEntryDetails.setTitle("Interpretação DOS termos");
                ptEntryDetails.setIsDelete(0);
                ptEntryDetails.setSort(1);
                ptEntryDetails.setLanguage("pt");
                entryDetailMapper.insertEntryDetail(ptEntryDetails);
                //俄语
                EntryDetails ruEntryDetails = new EntryDetails();
                ruEntryDetails.setDictionaryId(dictionary.getId());
                ruEntryDetails.setTitle("толкование терминов");
                ruEntryDetails.setIsDelete(0);
                ruEntryDetails.setSort(1);
                ruEntryDetails.setLanguage("ru");
                entryDetailMapper.insertEntryDetail(ruEntryDetails);
                //西班牙语
                EntryDetails esEntryDetails = new EntryDetails();
                esEntryDetails.setDictionaryId(dictionary.getId());
                esEntryDetails.setTitle("Interpretación terminológica");
                esEntryDetails.setIsDelete(0);
                esEntryDetails.setSort(1);
                esEntryDetails.setLanguage("es");
                entryDetailMapper.insertEntryDetail(esEntryDetails);
            }
        }
    }

    @Override
    public Integer getMaxSort(Integer dictionaryId, String language) {
        Integer maxSortByLanguageAndDic = entryDetailMapper.getMaxSortByLanguageAndDic(dictionaryId, language);
        if (maxSortByLanguageAndDic!=null){
            return maxSortByLanguageAndDic+1;
        }else {
            return +1;
        }
    }
}
